package com.example.shopsystem.service;

import com.example.shopsystem.dao.BusinessMapper;
import com.example.shopsystem.dao.GoodMapper;
import com.example.shopsystem.dao.OrderMapper;
import com.example.shopsystem.dao.UserMapper;
import com.example.shopsystem.entity.*;
import com.example.shopsystem.service.Impl.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    GoodMapper goodMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    BusinessMapper businessMapper;

    @Override
    public Boolean register(Business business) {//注册
        if (businessMapper.queryBusiness(business.getPhone()) != null) {
            return false;
        }
        if (!businessMapper.register(business)) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean signIn(Business business) {//登录
        Business business1 = businessMapper.queryBusiness(business.getPhone());
        if (business1 == null) {
            return false;
        }
        if (business1.getState() != 1) {
            return false;
        }

        if (!business1.getPwd().equals(business.getPwd())) {
            return false;
        }

        return true;
    }

    @Override
    public List<User> ViewInvitedCustomers(Business business) {//查看下级
        List<User> userList = businessMapper.ViewInvitedCustomers(business.getId());


        return userList;
    }

    @Override
    public List<Good> ViewProductList() {//查看商品
        return goodMapper.ViewProductList();

    }

    @Override
    public Boolean invitation(User user, String businessId) {//邀请注册
        user.setSuperiorId(businessId);
        BusCost busCost = new BusCost();
        Business business = businessMapper.queryBusiness(businessId);
        busCost.setBuId(businessId);
        busCost.setBalance(business.getBalance());
        Double money=business.getCommissionWay() * 50;
        busCost.setTransactionMoney(money);
        busCost.setTransactionType(3);
        if (!businessMapper.insertBusCost(busCost)) {
            return false;
        }
        business.setExtension(money);
        if(!businessMapper.busModifyInfo(business)){
            return false;
        }
        if (!userMapper.insertUser(user))
            return false;
        return true;
    }

    @Override
    public Boolean receiverRemind(Integer orderId) {//确认收货
        Order order = orderMapper.queryOneOrder(orderId);
        String address = businessMapper.queryBusiness(order.getBusId()).getAddress();
        System.out.println("1" + order);
        if (order.getUId() == null) {
            return false;
        }
        order.setState(4);
        System.out.println(order);
        if (!orderMapper.updateOrder(order)) {
            return false;
        }
        userMapper.insertCode(order.getUId(), "您的订单编号为" + order.getId() + "的订单已经送达，请尽快在" + address + "取货");
        return true;
    }

    @Override
    public Business busViewInfo(String businessId) {//查看信息
        return businessMapper.queryBusiness(businessId);
    }

    @Override
    public Boolean busModifyInfo(Business business) {//修改信息
        if (!businessMapper.busModifyInfo(completion(business))) {
            return false;
        }

        return true;
    }

    public Business completion(Business business) {//补全信息
        Business business1 = businessMapper.queryBusiness(business.getId());
        if (business.getPhone() == null) {
            business.setPhone(business1.getPhone());
        }
        if (business.getPwd() == null) {
            business.setPwd(business1.getPwd());
        }
        if (business.getAddress() == null) {
            business.setAddress(business1.getAddress());
        }
        if (String.valueOf(business.getBalance()) == null) {
            business.setBalance(business1.getBalance());
        }
        if (String.valueOf(business.getCommission()) == null) {
            business.setCommission(business1.getCommission());
        }
        if (String.valueOf(business.getMoneyPwd()) == null) {
            business.setMoneyPwd(business1.getMoneyPwd());
        }
        if (String.valueOf(business.getExtension()) == null) {
            business.setExtension(business1.getExtension());
        }
        if (business.getName() == null) {
            business.setName(business1.getName());
        }
        if (String.valueOf(business.getState()) == null) {
            business.setState(business1.getState());
        }
        System.out.println(business);
        return business;
    }

    @Transactional
    @Override
    public Boolean busWithdrawal(Business business, double money) {//提现
        if (money >= business.getBalance()) {//验证余额
            return false;
        }
        Double balance = business.getBalance() - money;
        business.setBalance(balance);
        if (!businessMapper.busModifyInfo(business)) {
            return false;
        }
        BusCost busCost = new BusCost();
        busCost.setBuId(business.getId());
        busCost.setBalance(balance);
        busCost.setTransactionMoney(money);
        busCost.setTransactionType(1);
        if (!businessMapper.insertBusCost(busCost)) {
            return false;
        }
        return true;
    }

    @Override
    public List<BusCost> viewCostList(String businessId) {
        return businessMapper.viewBusCost(businessId);
    }

    @Override
    public Boolean busModifyCom(Order order) {//提成
        Business business = businessMapper.queryBusiness(order.getBusId());
        if (business == null) {
            return false;
        }
        double commission = business.getCommissionWay() * order.getActualPayment();
        double money = business.getBalance() + commission;
        business.setBalance(money);
        business.setCommission(commission);
        BusCost busCost = new BusCost();
        busCost.setTransactionType(3);
        busCost.setBalance(money);
        busCost.setTransactionMoney(commission);
        busCost.setBuId(business.getId());
        if (!businessMapper.insertBusCost(busCost)) {
            return false;
        }
        if (!businessMapper.busModifyInfo(business)) {
            return false;
        }

        return true;
    }

    public Boolean updateMoneyPwd(int money, String businessId) {
        if (!businessMapper.updateMoneyPwd(money, businessId)) {
            return false;
        }
        return true;
    }

}
